﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Common;
using AvatarHelper;
using Avatar.Model;
using System.Data.Linq;

public partial class news : System.Web.UI.Page
{
    int _take = 20;
    int _categoryID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddl_NewsCategory.DataBind();
            //_categoryID = Request.QueryString["category"] == null ? 0 : Convert.ToInt32(Request.QueryString["category"].ToString());
            this.NewsBind(0, _categoryID);
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        _categoryID = Convert.ToInt32(ddl_NewsCategory.SelectedValue);
        this.NewsBind(0, _categoryID);
    }
    protected void NewsBind(int _page, int _categoryID)
    {
        try
        {
            lbl_Current.Text = (_page + 1).ToString();
            using (var _adc = DBHelper.GetDataContext())
            {
                int _date = Convert.ToInt32(ddl_Date.SelectedValue);
                string _keyWord = txt_KeyWord.Text;

                if (_categoryID > 0)
                {
                    lbl_Total.Text = (_adc.News.Count(n => n.CategoryID == _categoryID && n.PublishDate > DateTime.Now.AddDays(-_date) && (n.Title.Contains(_keyWord) || n.Content.Contains(_keyWord))) / _take + 1).ToString();
                    ListView1.DataSource = _adc.News.Where(n => n.CategoryID == _categoryID && n.PublishDate > DateTime.Now.AddDays(-_date) && (n.Title.Contains(_keyWord) || n.Content.Contains(_keyWord))).OrderByDescending(n => n.PublishDate).Skip(_take * _page).Take(_take);
                }
                else
                {
                    lbl_Total.Text = (_adc.News.Count(n => n.PublishDate > DateTime.Now.AddDays(-_date) && (n.Title.Contains(_keyWord) || n.Content.Contains(_keyWord))) / _take + 1).ToString();
                    ListView1.DataSource = _adc.News.Where(n => n.PublishDate > DateTime.Now.AddDays(-_date) && (n.Title.Contains(_keyWord) || n.Content.Contains(_keyWord))).OrderByDescending(n => n.PublishDate).Skip(_take * _page).Take(_take);
                }
                ListView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "" + _e.Message;
        }
    }
    protected void btn_Up_Click(object sender, EventArgs e)
    {
        try
        {
            var _page = Convert.ToInt32(lbl_Current.Text) - 1;
            if (_page > 0)
                this.NewsBind(_page - 1, _categoryID);
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "" + _e.Message;
        }
    }
    protected void btn_Down_Click(object sender, EventArgs e)
    {
        try
        {
            var _page = Convert.ToInt32(lbl_Current.Text) - 1;
            if (_page < Convert.ToInt32(lbl_Total.Text) - 1)
                this.NewsBind(_page + 1, _categoryID);
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "" + _e.Message;
        }

    }
    protected void btn_Goto_Click(object sender, EventArgs e)
    {
        try
        {
            int _page = Convert.ToInt32(txt_KeyWord.Text);
            if (_page > 0 && _page < Convert.ToInt32(lbl_Total.Text) - 1) this.NewsBind(_page, _categoryID);
        }
        catch (Exception _e)
        { lbl_Error.Text = "错误：" + _e.Message; }
    }
}
